import request from '@/utils/request'

// ==================== 接口类型定义 ====================

/**
 * 创建订单请求参数接口
 */
export interface CreateOrderParams {
  address_id: number
  cart_item_ids: number[]
  remark?: string
}

/**
 * 订单详情响应接口
 */
export interface OrderDetail {
  order_id: number
  order_no: string
  total_amount: string
  payment_amount: string
  shipping_fee?: string
  order_status: number
  payment_type?: number | null
  payment_time?: string | null
  shipping_time?: string | null
  completion_time?: string | null
  receiver_name: string
  receiver_mobile: string
  receiver_address: string
  remark?: string
  create_time: string
  update_time: string
  user?: number
  items: {
    item_id: number
    order: number
    goods: number
    goods_name: string
    goods_cover_img: string
    goods_price: string
    goods_num: number
    goods_total_price: string
    create_time: string
  }[]
  address_info?: {
    receiver_name: string
    receiver_mobile: string
    receiver_address: string
  }
  order_status_text?: string
  payment_type_text?: string | null
}

/**
 * 订单列表项接口
 */
export interface OrderListItem {
  order_id: number
  order_no: string
  total_amount: string
  payment_amount: string
  order_status: number
  order_status_text?: string // 文本状态描述
  create_time: string
  items: {
    goods: number
    goods_name: string
    goods_cover_img: string
    goods_price: string
    goods_num: number
  }[]
}

/**
 * 订单列表响应接口
 */
export interface OrderListResponse {
  count: number
  next: string | null
  previous: string | null
  results: OrderListItem[]
}
/**
 * 订单状态枚举
 */
export enum OrderStatus {
  PendingPayment = 1, // 待付款
  Paid = 2, // 已付款
  Shipped = 3, // 已发货
  Completed = 4, // 已完成
  Delivered = 5, // 待收货
  Cancelled = 6, // 已取消
  Refunded = 7, // 已退款
}

// ==================== API 服务 ====================

/**
 * 订单API服务
 */
export const ordersApi = {
  /**
   * 创建订单
   * POST /orders/
   * @param params - 创建订单参数
   */
  createOrder(params: CreateOrderParams): Promise<OrderDetail> {
    return request({
      url: '/orders/',
      method: 'post',
      data: params,
    })
  },

  /**
   * 获取订单详情
   * GET /orders/{order_id}/
   * @param order_id - 订单ID
   */
  getOrderDetail(order_id: number): Promise<OrderDetail> {
    return request({
      url: `/orders/${order_id}/`,
      method: 'get',
    })
  },

  /**
   * 获取订单列表
   * GET /orders/
   * @param params - 查询参数
   */
  getOrderList(params?: { page?: number; page_size?: number }): Promise<OrderListResponse> {
    return request({
      url: '/orders/',
      method: 'get',
      params,
    })
  },

  /**
   * 取消订单
   * POST /orders/{order_id}/cancel/
   * @param order_id - 订单ID
   */
  cancelOrder(order_id: number): Promise<any> {
    return request({
      url: `/orders/${order_id}/cancel/`,
      method: 'post',
    })
  },

  /**
   * 确认收货
   * POST /orders/{order_id}/confirm/
   * @param order_id - 订单ID
   */
  confirmOrder(order_id: number): Promise<any> {
    return request({
      url: `/orders/${order_id}/confirm/`,
      method: 'post',
    })
  },
}

// ==================== 工具函数 ====================

/**
 * 获取订单状态名称
 */
export const getOrderStatusName = (status: OrderStatus): string => {
  const statusNames: Record<OrderStatus, string> = {
    [OrderStatus.PendingPayment]: '待付款',
    [OrderStatus.Paid]: '已付款',
    [OrderStatus.Shipped]: '已发货',
    [OrderStatus.Completed]: '已完成',
    [OrderStatus.Delivered]: '待收货',
    [OrderStatus.Cancelled]: '已取消',
    [OrderStatus.Refunded]: '已退款',
  }
  return statusNames[status] || '未知状态'
}

/**
 * 格式化金额
 */
export const formatAmount = (amount: string): string => {
  return parseFloat(amount).toFixed(2)
}

// ==================== 默认导出 ====================

export default ordersApi
